package cn.edu.cimv.service.impl;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.entity.*;
import cn.edu.cimv.dao.mapper.*;
import cn.edu.cimv.pojo.*;
import cn.edu.cimv.service.CimvService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by 门铎 on 2016/8/3.
 */
@Service
public class CimvServiceImpl implements CimvService {
    @Autowired private LatestMapper latestMapper;
    @Autowired private NewsMapper newsMapper;
    @Autowired private NoticesMapper noticesMapper;
    @Autowired private ProjectMapper projectMapper;
    @Autowired private RecruitMapper recruitMapper;
    @Autowired private ContentMapper contentMapper;
    @Autowired private FlinkMapper flinkMapper;
    @Autowired private AwardsMapper awardsMapper;
    @Autowired private HonorMapper honorMapper;
    @Autowired private PaperMapper paperMapper;
    @Autowired private PatentMapper patentMapper;
    @Autowired private UserMapper userMapper;
    @Autowired private CimvMapper cimvMapper;
    @Autowired private OptionMapper optionMapper;
    @Autowired private StudentMapper studentMapper;
    @Override
    public List<News> DisplayNewsModule() {
        return newsMapper.queryNewsLastFour(InfoConstant.NEWS_COUNT,0);
    }


    @Override
    public List<Object> DisplayLatestModule() {
        List<Object> objList = new ArrayList<>();
        //最新动态是可以发布的科研成果还有学术交流部分可以放到首页的部分最好带封面
        List<News> newsList = newsMapper.queryNewsByFlag(1);
        for (News news: newsList) {
            news.setSt("news");
        }
       /* List<News> projectList = projectMapper.queryProjectByFlag(1);
        for (News news: projectList) {
            news.setSt("project");
        }*/
        objList.addAll(newsList);
        /*objList.addAll(projectList);*/
        //生序排序
        Collections.sort(objList, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                News n1 = (News) o1;
                News n2 = (News) o2;
                return n1.getDate().compareTo(n2.getDate());
            }
        });
        Collections.reverse(objList);
        List<Object> returnList;
        if(objList.size()>=4){
            returnList = objList.subList(0,4);
        }else{
            returnList = objList;
        }

        return returnList;
    }

    @Override
    public Page<Latest> DisplayLatestList(int pageNum) {
        List<Latest> latestList= latestMapper.queryLatest();
        Page<Latest> page = new Page<>(pageNum, InfoConstant.PAGE_SIZE,latestList);
        return page;
    }

    @Override
    public Page<News> DisplayNewsList(int pageNum) {
        List<News> newsList = newsMapper.queryNews(0);
        Page<News> page = new Page<>(pageNum, InfoConstant.PAGE_SIZE,newsList);
        return page;
    }

    @Override
    public List<News> DisplayNoticesModule() {
        return newsMapper.queryNewsLastFour(InfoConstant.NOTICES_COUNT,1);
    }

    @Override
    public Page<News> DisplayNoticesList(int pageNum) {
        List<News> noticesList  = newsMapper.queryNews(1);
        Page<News> page = new Page<>(pageNum,InfoConstant.PAGE_SIZE,noticesList);
        return page;
    }

    @Override
    public List<News> DisplayProjectModule() {
       /* return projectMapper.queryProjectLast(InfoConstant.PROJECT_COUNT);*/
        return null;
    }

    @Override
    public Page<Project> DisplayProjectList(int pageNum,String projectType) {
        List<Project> projectList  = projectMapper.queryProjectByType(projectType);
        Page<Project> page = new Page<>(pageNum,InfoConstant.PAGE_SIZE,projectList);
        return page;
    }

    @Override
    public List<Recruit> DisplayRecruitModule() {
        return recruitMapper.queryRecruitLast(InfoConstant.RECRUIT_COUNT);
    }

    @Override
    public List<Flink> DisplayFlinkModule() {
        return flinkMapper.queryFlinkLast(InfoConstant.FLINK_COUNT);
    }

    @Override
    public Page<Recruit> DisplayRecruitList(int pageNum) {
        List<Recruit> recruitList  = recruitMapper.queryRecruit();
        Page<Recruit> page = new Page<>(pageNum,InfoConstant.PAGE_SIZE,recruitList);
        return page;
    }

    @Override
    public Cata DisplayCata(String name) {
        //根据name 找到id
        name = "single/" + name;
        String id = contentMapper.getMenuId(name);
        return contentMapper.getCata(id);
    }

    @Override
    public Page<Awards> DisplayAwardsTable(int pageNum) {
        List<Awards> awardsList  = awardsMapper.queryAwards();
        Page<Awards> page = new Page<>(pageNum,InfoConstant.PAGE_SIZE,awardsList);
        return page;
    }
    @Override
    public Page<Honor> DisplayHonorTable(int pageNum) {
        List<Honor> honorList  = honorMapper.queryHonor();
        Page<Honor> page = new Page<>(pageNum,InfoConstant.PAGE_SIZE,honorList);
        return page;
    }

    @Override
    public Page<Paper> DisplayPaperTable(int pageNum,String paperType) {
        List<Paper> paperList  = paperMapper.queryPaperByPaperType(paperType);
        Page<Paper> page = new Page<>(pageNum,InfoConstant.PAGE_SIZE,paperList);
        return page;
    }

    @Override
    public Page<Patent> DisplayPatentTable(int pageNum) {
        List<Patent> patentList  = patentMapper.queryPatent();
        Page<Patent> page = new Page<>(pageNum,InfoConstant.PAGE_SIZE,patentList);
        return page;
    }

    @Override
    public Page<Member> DisplayMemberList(int pageNum) {
        List<Member> memberList  = userMapper.selectAllMember();
        Page<Member> page = new Page<>(pageNum,InfoConstant.MEMBER_SIZE,memberList);
        return page;
    }

    @Override
    public List<StudentOuter> DisplayStudents(int flag) {
        //分类 --（年份 学生List）
        List<StudentOuter> studentOuterList = new ArrayList<>();
        List<String> sList = studentMapper.queryStudentHighFlag(flag);
        for (String s:sList){
            StudentOuter studentOuter = new StudentOuter();
            if("1".equals(s)){
                studentOuter.setHighStudy(1);
                studentOuter.setType("博士生");
            }else{
                studentOuter.setHighStudy(0);
                studentOuter.setType("研究生");
            }
            studentOuterList.add(studentOuter);
        }

        for (StudentOuter s:studentOuterList){
              //年份 （）
            List<StudentYear> studentYearList = new ArrayList<>();
            List<String> yearList = studentMapper.queryStudentStudyYear(s.getHighStudy(),flag);

            for (String m:yearList){
                StudentYear  studentYear= new StudentYear();
                studentYear.setYear(m);
                studentYear.setStudentList(studentMapper.queryStudentByYearAndHighStudy(s.getHighStudy(),m,flag));
                studentYearList.add(studentYear);
            }
            s.setStudentYearList(studentYearList);
        }

        return studentOuterList;
    }

    @Override
    public List<PaperOuter> DisplayPaper() {
        //分类 --（年份 学生List）
        List<PaperOuter> paperOuterList = new ArrayList<>();
        List<Option> sList = paperMapper.queryPaperOid();
        for (Option s:sList) {
            PaperOuter paperOuter = new PaperOuter();
            //设置大标题
            paperOuter.setTitle(s.getTitle());
            paperOuter.setOid(s.getId());
            paperOuterList.add(paperOuter);
        }
        for (PaperOuter p:paperOuterList){
            List<PaperYear> paperYearList = new ArrayList<>();
            List<String> yearList = paperMapper.queryPapePaperYear(p.getOid());
            for (String y:yearList) {
                PaperYear paperYear = new PaperYear();
                paperYear.setYear(y);
                List<Paper> paperList = paperMapper.queryPaperByPaperYearAndOid(p.getOid(), y, 0);
                paperYear.setPaperList(paperList);
                List<Paper> conferenceList = paperMapper.queryPaperByPaperYearAndOid(p.getOid(), y, 1);
                paperYear.setConferenceList(conferenceList);
                paperYearList.add(paperYear);
            }
            p.setPaperYearList(paperYearList);
        }
        return paperOuterList;
    }

    @Override
    public FontMenuSet getRelMenu(String menu) {
        FontMenuSet fontMenuSet = new FontMenuSet();
        Set<FontMenu> p = new HashSet<>();
        //根据menu找到id
        FontMenu fontMenu = cimvMapper.getFontMenuByMenu(menu);
        fontMenuSet.setFontMenu(fontMenu);
        //根据id 找到ref_id
        String refIds = cimvMapper.getRefIdById(fontMenu.getId());
        String[] ids = refIds.split(":");
        for (String id:ids) {
            FontMenu proc = new FontMenu();
            proc = cimvMapper.getFontMenuById(id);
            p.add(proc);
        }
        List<FontMenu> fl = new ArrayList<>();
        fl.addAll(p);
        Collections.sort(fl, new Comparator<FontMenu>() {
            @Override
            public int compare(FontMenu o1, FontMenu o2) {
                if (o1.getId()>o2.getId()){
                    return 1;
                }else{
                    return -1;
                }
            }
        });
        fontMenuSet.setFontMenuSet(fl);
        //根据ref_id 找到对应数据

        return fontMenuSet;
    }

    @Override
    public Map<String, List<Direction>> getOptionUnionDirection(String type) {
        Map<String,List<Direction>>  odmap = new HashMap<>();
        List<Option> op = optionMapper.queryTitles(type);
        for (Option o:op) {
            List<Direction> dlist = optionMapper.queryDirectionsByOid(o.getId());
            odmap.put(String.valueOf(o.getId()),dlist);
        }
        return  odmap;
    }
}
